import re

from cache.channel import AllChannelCache


def check_mobile(value):

    if not re.match(r"1[3-9]\d{9}",value):
        # 手机号不满足正则,抛出异常
        raise ValueError("手机号不满足格式")

    return value


def check_channel_id(value):
    """
    检查是否是频道id
    :param value:
    :return:
    """
    try:
        channel_id = int(value)
    except Exception:
        raise  ValueError("频道id不正确")
    else:
        if channel_id < 0:
            raise ValueError("频道值不能小于0")
        if channel_id == 0:
            # 在这里返回的是频道id 1 的数据
            return 1
        else:
        # 判断是否在频道内
            ids = AllChannelCache.get_channel_ids(channel_id)
        if ids:
            return channel_id
        else:
            raise ValueError("频道值不正确")
